package com.beisita.config;

import com.beisita.filter.TestFilterOrder1Filter;
import com.beisita.filter.TestFilterOrder2Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;


@Configuration
public class FilterConfig {
    //    这种方式不能自动装配,注解方式可以
//    同时有配置的类和注解方式的话，filter顺序：
//    首先是用配置文件配置的Filter（按照Order顺序），然后是WebFilter注解配置的。
    @Bean
    public FilterRegistrationBean<TestFilterOrder1Filter> testFilterOrder() {
        FilterRegistrationBean<TestFilterOrder1Filter> sessionExpireFilterFilterRegistrationBean = new FilterRegistrationBean<>(new TestFilterOrder1Filter());
        sessionExpireFilterFilterRegistrationBean.setName("testFilterOrder");
        ArrayList<String> urls = new ArrayList<>();
        urls.add("*.do");
        sessionExpireFilterFilterRegistrationBean.setUrlPatterns(urls);
        sessionExpireFilterFilterRegistrationBean.setOrder(3);
        return sessionExpireFilterFilterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<TestFilterOrder2Filter> testFilterOrder2() {
        FilterRegistrationBean<TestFilterOrder2Filter> sessionExpireFilterFilterRegistrationBean = new FilterRegistrationBean<>(new TestFilterOrder2Filter());
        sessionExpireFilterFilterRegistrationBean.setName("testFilterOrder2");
        ArrayList<String> urls = new ArrayList<>();
        urls.add("*.do");
        sessionExpireFilterFilterRegistrationBean.setUrlPatterns(urls);
        sessionExpireFilterFilterRegistrationBean.setOrder(2);
        return sessionExpireFilterFilterRegistrationBean;
    }
}
